﻿using System.Runtime.CompilerServices;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.Validate;

namespace Shop.RemoteModel.ActivityDiscount.Model
{
    public class DiscountItem
    {
        /// <summary>
        /// 优惠内容
        /// </summary>
        [EnumValidate("shop.activity.mode.error", typeof(DiscountContent))]
        public DiscountContent Mode { get; set; }
        /// <summary>
        /// 是否包邮
        /// </summary>
        [EntrustValidate("_Check")]
        public bool IsFreeMail { get; set; }

        /// <summary>
        /// 门槛价
        /// </summary>
        [NumValidate("shop.activity.threshold.price.error", 0.1)]
        public decimal? ThresholdPrice { get; set; }

        /// <summary>
        /// 门槛数
        /// </summary>
        [NumValidate("shop.activity.threshold.num.error", 1)]
        public int? ThresholdNum { get; set; }

        /// <summary>
        /// 减多少
        /// </summary>
        [NumValidate("shop.activity.threshold.num.error", 0.1)]
        public decimal? ReducePrice { get; set; }

        /// <summary>
        /// 打折比例
        /// </summary>
        [NumValidate("shop.activity.discount.ratio.error", 1, 100)]
        public short? DiscountRatio { get; set; }
        /// <summary>
        /// 最低价商品折扣价
        /// </summary>
        [NumValidate("shop.activity.discount.price.error", 0.1)]
        public decimal? DiscountPrice { get; set; }
        /// <summary>
        /// 减免总价
        /// </summary>
        [NumValidate("shop.activity.threshold.total.price.error", 0.1)]
        public decimal? ReduceTotalPrice { get; set; }

        /// <summary>
        /// 减件数
        /// </summary>
        [NumValidate("shop.activity.reduce.num.error", 1)]
        public int? ReduceNum { get; set; }

        /// <summary>
        /// 加多少元
        /// </summary>
        [NumValidate("shop.activity.add.price.error", 0.1)]
        public decimal? AddPrice { get; set; }
        /// <summary>
        /// 送多少
        /// </summary>
        [NumValidate("shop.activity.give.num.error", 1)]
        public int? GiveNum { get; set; }

        /// <summary>
        /// 优惠卷
        /// </summary>
        [NumValidate("shop.activity.give.coupon.error", 1)]
        public long? CouponId { get; set; }

        /// <summary>
        /// 赠品ID
        /// </summary>
        [NumValidate("shop.activity.give.sku.error", 1)]
        public long? GiveSkuId { get; set; }
        /// <summary>
        /// 赠品或优惠卷标题
        /// </summary>
        public string GiveName { get; set; }

        /// <summary>
        /// 检查数据
        /// </summary>
        [MethodImplAttribute(MethodImplOptions.NoOptimization)]
        private static void _Check (DiscountItem item, DiscountSet parent)
        {
            if (parent.Threshold == DiscountThreshold.无门槛)
            {
                if (item.Mode != DiscountContent.送赠品 && item.Mode != DiscountContent.送优惠卷)
                {
                    throw new ErrorException("shop.activity.discount.mode.error");
                }
                else if (item.Mode == DiscountContent.送赠品 && !item.GiveSkuId.HasValue)
                {
                    throw new ErrorException("shop.activity.discount.give.null");
                }
                else if (item.Mode == DiscountContent.送优惠卷 && !item.CouponId.HasValue)
                {
                    throw new ErrorException("shop.activity.discount.coupon.null");
                }
                else if (item.Mode == DiscountContent.送赠品 && !item.GiveNum.HasValue)
                {
                    throw new ErrorException("shop.activity.discount.give.num.null");
                }
            }
            else if (parent.Threshold == DiscountThreshold.每满减促销)
            {
                if (!item.ThresholdPrice.HasValue)
                {
                    throw new ErrorException("shop.activity.threshold.price.error");
                }
                if (item.Mode != DiscountContent.减金额)
                {
                    throw new ErrorException("shop.activity.discount.mode.error");
                }
                else if (!item.ReducePrice.HasValue)
                {
                    throw new ErrorException("shop.activity.discount.reduce.price.null");
                }
                else if (item.ReduceTotalPrice.HasValue && item.ReduceTotalPrice.Value < item.ReducePrice.Value)
                {
                    throw new ErrorException("shop.activity.discount.reduce.total.price.error");
                }
            }
            else if (parent.Threshold == DiscountThreshold.满减促销)
            {
                if (!item.ThresholdPrice.HasValue)
                {
                    throw new ErrorException("shop.activity.threshold.price.error");
                }
                if (item.Mode == DiscountContent.无 && item.IsFreeMail == false)
                {
                    throw new ErrorException("shop.activity.discount.mode.error");
                }
                else if (item.Mode == DiscountContent.任选 && !item.GiveNum.HasValue)
                {
                    throw new ErrorException("shop.activity.discount.give.num.null");
                }
                else if (item.Mode == DiscountContent.送优惠卷 && !item.CouponId.HasValue)
                {
                    throw new ErrorException("shop.activity.discount.coupon.null");
                }
                else if (( item.Mode == DiscountContent.送赠品 || item.Mode == DiscountContent.加多少元送赠品 ) && !item.GiveSkuId.HasValue)
                {
                    throw new ErrorException("shop.activity.discount.give.null");
                }
                else if (item.Mode == DiscountContent.减金额 && !item.ReducePrice.HasValue)
                {
                    throw new ErrorException("shop.activity.discount.reduce.price.null");
                }
                else if (item.Mode == DiscountContent.加多少元送赠品 && !item.AddPrice.HasValue)
                {
                    throw new ErrorException("shop.activity.discount.add.price.null");
                }
            }
            else if (parent.Threshold == DiscountThreshold.满件促销)
            {
                if (!item.ThresholdNum.HasValue)
                {
                    throw new ErrorException("shop.activity.threshold.num.error");
                }
                else if (item.Mode == DiscountContent.无 && item.IsFreeMail == false)
                {
                    throw new ErrorException("shop.activity.discount.mode.error");
                }
                else if (item.Mode == DiscountContent.减金额)
                {
                    if (!item.ReducePrice.HasValue)
                    {
                        throw new ErrorException("shop.activity.threshold.price.null");
                    }
                }
                else if (item.Mode == DiscountContent.打折)
                {
                    if (!item.DiscountRatio.HasValue)
                    {
                        throw new ErrorException("shop.activity.discount.ratio.null");
                    }
                }
                else if (item.Mode == DiscountContent.最低价商品享)
                {
                    if (!item.DiscountPrice.HasValue)
                    {
                        throw new ErrorException("shop.activity.discount.price.null");
                    }
                }
                else if (item.Mode == DiscountContent.减件)
                {
                    if (!item.ReduceNum.HasValue)
                    {
                        throw new ErrorException("shop.activity.reduce.num.null");
                    }
                }
                else
                {
                    throw new ErrorException("shop.activity.discount.mode.error");
                }
            }
            else
            {
                throw new ErrorException("shop.activity.threshold.error");
            }
        }
    }
}
